package com.mrogrape.notepad.ui;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.LinkedList;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;

import com.mrogrape.notepad.activity.MainFrame;
import com.mrogrape.notepad.common.NotepadConstants;
import com.mrogrape.notepad.data.ViewContext;
import com.mrogrape.notepad.resource.Settings;
import com.mrogrape.notepad.ui.editor.TextEditor;
import com.mrogrape.util.StringUtils;


public class RecentFileMenu extends JMenu {

    /** serialVersionUID */
    private static final long serialVersionUID = 1L;

    public RecentFileMenu(String text){
	super(text);

	this.recentFileList = Settings.getSettings().getRecentFileList();
	this.initComponents();
	this.rebuildMenu();
    }

    public void loadOrSaveFile(File file){

	String path = file.getAbsolutePath();

	if(this.recentFileList.contains(path)){

	    this.recentFileList.remove(path);
	}

	this.recentFileList.addFirst(path);
	this.rebuildMenu();
    }

    private void initComponents(){

	this.configMenuItem = new JMenuItem("Edit Recent File List...");
	this.configMenuItem.addActionListener(new ConfigActionListener());

	this.emptyMenuItem = new JMenuItem("(No items)");
	this.emptyMenuItem.setEnabled(false);
    }

    private void rebuildMenu(){

	this.removeAll();

	if(this.recentFileList.size() == 0){

	    this.add(this.emptyMenuItem);
	}
	else{

	    for(String item : this.recentFileList){

		this.add(new RecentMenuItem(item, this.recentItemsActionListener));
	    }
	}

	this.add(this.separaotr);
	this.add(this.configMenuItem);
    }

    private JMenuItem configMenuItem;
    private JMenuItem emptyMenuItem;
    private JSeparator separaotr = ComponentFactory.createMenuSeparator();

    private LinkedList<String> recentFileList;
    private RecentItemActionListener recentItemsActionListener = new RecentItemActionListener();

    private static class RecentItemActionListener implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {

	    RecentMenuItem item = (RecentMenuItem) e.getSource();
	    File file = new File(item.getPath());

	    MainFrame frame = ViewContext.getMainFrame();
	    for(TextEditor editor : frame.getAllTextEditors()){

		if(editor.getFile()!=null && editor.getFile().getAbsolutePath().equals(item.getPath())){

		    frame.setSelectedWorkPanel(editor);
		    return;
		}
	    }

	    TextEditor editor = frame.createNewEditor(file);
	    frame.setSelectedWorkPanel(editor);
	}
    }

    private class ConfigActionListener implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {

	    SortItemDialog dialog = new SortItemDialog((Frame)ViewContext.getMainFrame(),"Edit Recent Files.(Capacity: " + NotepadConstants.RECENT_MENU_MAX_RECORDS + ")", RecentFileMenu.this.recentFileList);
	    dialog.setVisible(true);
	    RecentFileMenu.this.rebuildMenu();
	}
    }

    /**
     * 最近使用菜单项。
     * 
     * @author IceSpring
     *
     */
    private static class RecentMenuItem extends JMenuItem{

	/** serialVersionUID */
	private static final long serialVersionUID = 1L;

	public RecentMenuItem(String path, ActionListener handler){

	    this.path = path;
	    this.setText(StringUtils.cutString(path, NotepadConstants.RECENT_MENU_ITEM_TEXT_MAX_LENGTH, StringUtils.CUTSTRING_MIDDLE));
	    this.addActionListener(handler);
	}

	/**
	 * 获取此项目的真正路径
	 * @return
	 */
	public String getPath(){

	    return this.path;
	}

	private String path = null;
    }
}
